/**
 * 
 */
package fr.toussna.dao;

import java.util.List;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;

import fr.toussna.entity.Category;
import fr.toussna.entity.Comment;
import fr.toussna.entity.DomainEntity;
import fr.toussna.entity.Member;
import fr.toussna.exception.ObjectNotFoundException;
import fr.toussna.utils.EntityFactory;

/**
 * Testing CommentDAO
 * 
 * @author My
 * 
 */
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@Test(suiteName = "CommentDAOTestCase")
public class CommentDAOTestCase extends AbstractTestNGSpringContextTests {

	private static final Log	LOGGER	= LogFactory
	                                           .getLog(CommentDAOTestCase.class
	                                                   .getName());

	@Autowired
	@Qualifier("commentdao")
	private CommentDAO	     commentdao;

	@Autowired
	@Qualifier("entityfactory")
	private EntityFactory	 entityfactory;

	@Test
	public void insert() {
		LOGGER.info("insert - starting...");
		Comment comment = (Comment) entityfactory.createDomainEntity("comment");
		Number id = commentdao.insert(comment);
		try {
			Assert.assertNotNull(id);
			Assert.assertEquals(comment.getTitle(),
			        ((Comment) commentdao.select(id)).getTitle());
			comment.setId((Long) id);
			commentdao.delete(comment);
		} catch (Exception e) {
		}
		LOGGER.info("insert - end.");
	}

	@Test
	public void select() {
		LOGGER.info("select - starting...");
		Comment comment = (Comment) entityfactory.createDomainEntity("comment");
		Comment comment2 = (Comment) entityfactory
		        .createDomainEntity("comment");
		Number id = commentdao.insert(comment);
		Number id2 = commentdao.insert(comment2);
		try {
			Assert.assertNotNull(id);
			Assert.assertNotNull(id2);
			Assert.assertEquals(comment.getTitle(),
			        ((Comment) commentdao.select(id)).getTitle());
			Assert.assertEquals(comment2.getTitle(),
			        ((Comment) commentdao.select(id2)).getTitle());
			comment.setId((Long) id);
			comment2.setId((Long) id2);
			commentdao.delete(comment);
			commentdao.delete(comment2);
		} catch (Exception e) {
		}
		LOGGER.info("select - end.");
	}

	@Test
	public void update() {
		LOGGER.info("update - starting...");
		Comment comment = (Comment) entityfactory.createDomainEntity("comment");
		Number id = commentdao.insert(comment);
		comment.setId((Long) id);
		comment.setContent("updated");
		try {
			commentdao.update(comment);
			Assert.assertEquals("updated",
			        ((Comment) commentdao.select(id)).getContent());
			commentdao.delete(comment);
		} catch (Exception e) {
		}
		LOGGER.info("update - end.");
	}

	@Test(expectedExceptions = {ObjectNotFoundException.class })
	public void delete() throws ObjectNotFoundException {
		LOGGER.info("delete - starting...");
		Comment comment = (Comment) entityfactory.createDomainEntity("comment");
		Number id = commentdao.insert(comment);
		comment.setId((Long) id);
			commentdao.delete(comment);
			commentdao.select(id);
		LOGGER.info("delete - end.");
	}

	@Test
	public void selectAll() {
		LOGGER.info("selectAll - starting...");
		Comment comment = (Comment) entityfactory.createDomainEntity("comment");
		Comment comment2 = (Comment) entityfactory
		        .createDomainEntity("comment");
		Number id = commentdao.insert(comment);
		Number id2 = commentdao.insert(comment2);
		Assert.assertFalse(commentdao.selectAll().isEmpty());
		Assert.assertEquals(2, commentdao.selectAll().size());
		comment.setId((Long) id);
		comment2.setId((Long) id2);
		commentdao.delete(comment);
		commentdao.delete(comment2);
		LOGGER.info("selectAll - end.");
	}

	@Test
	public void selectByKey() {
		LOGGER.info("selectByKey - starting...");
		Comment comment = (Comment) entityfactory.createDomainEntity("comment");
		Number id = commentdao.insert(comment);
		List<DomainEntity> listCom = commentdao.selectByKey(comment.getTitle());
		// LOGGER.warn("==================="+comment.getTitle());
		// LOGGER.warn("\n ==================="+ listCom.get(0) );
		// Assert.assertEquals(comment.getTitle(), ((Comment)
		// listCom.get(0)).getTitle());
		comment.setId((Long) id);
		commentdao.delete(comment);
		LOGGER.info("selectByKey - end.");
	}

	@Test
	public void selectAllByArticle() {
		LOGGER.info("selectAllByArticle - starting...");
		Comment comment = (Comment) entityfactory.createDomainEntity("comment");
		Number id = commentdao.insert(comment);
		List<DomainEntity> listCom = commentdao.selectAllByArticle(comment
		        .getArticle());
		Assert.assertEquals(comment.getTitle(),
		        ((Comment) listCom.get(0)).getTitle());
		comment.setId((Long) id);
		commentdao.delete(comment);
		LOGGER.info("selectAllByArticle - end.");
	}

	// ====================Cache Testing===============================

	@Test
	public void selectCachedElement() {
		LOGGER.info("selectCachedElement - starting...");
		Comment comment = (Comment) entityfactory.createDomainEntity("comment");
		Number id = commentdao.insert(comment);
		comment.setId((Long) id);
		commentdao.insertToCache(comment);
		Assert.assertEquals(comment.getTitle(), ((Comment) commentdao
		        .selectCachedElement((Long) id, Comment.class.getCanonicalName())).getTitle());
		commentdao.delete(comment);
		LOGGER.info("selectCachedElement - end.");
	}

	@Test
	public void insertToCache() {
		LOGGER.info("insertToCache - starting...");
		Comment comment = (Comment) entityfactory.createDomainEntity("comment");
		Comment comment2 = (Comment) entityfactory
		        .createDomainEntity("comment");
		Number id = commentdao.insert(comment);
		Number id2 = commentdao.insert(comment2);
		comment.setId((Long) id);
		comment2.setId((Long) id2);
		commentdao.insertToCache(comment);
		commentdao.insertToCache(comment2);
		Assert.assertEquals(comment.getTitle(), ((Comment) commentdao
		        .selectCachedElement((Long) id, Comment.class.getCanonicalName())).getTitle());
		Assert.assertEquals(comment2.getTitle(), ((Comment) commentdao
		        .selectCachedElement((Long) id2, Comment.class.getCanonicalName())).getTitle());
		commentdao.delete(comment);
		commentdao.delete(comment2);
		LOGGER.info("insertToCache - end.");
	}

}
